package net.m510.autosurfer;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import net.m510.autosurfer.common.cli.OutFileOption;
import net.m510.autosurfer.common.cli.PasswordOption;
import net.m510.autosurfer.core.AbstractMain;
import net.m510.autosurfer.core.UserAgents;
import net.m510.autosurfer.core.exception.UserProcessException;
import net.m510.autosurfer.mhbk.MhbkDownload;
import net.m510.autosurfer.mhbk.MhbkGoDealDetail;
import net.m510.autosurfer.mhbk.MhbkGoTop;
import net.m510.autosurfer.mhbk.MhbkLoginInputPassword;
import net.m510.autosurfer.mhbk.MhbkLoginInputUserNum;
import net.m510.autosurfer.mhbk.MhbkLoginPasswordChecker;
import net.m510.autosurfer.mhbk.MhbkLoginUserNumChecker;
import net.m510.autosurfer.mhbk.MhbkLogout;
import net.m510.autosurfer.mhbk.MhbkProcessRetryer;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.nodes.Document;

public class MhbkGetMain extends AbstractMain {
	private static final Logger log = Logger.getLogger(MhbkGetMain.class.getName());
	private static final String DEFAULT_MEISAI_FILENAME="meisai.csv";
	
	String userNum;
	private String password;
	private File outFile;
	
	private Document downloadDoc;
	/**
	 * 
	 */
	public MhbkGetMain() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MhbkGetMain main = new MhbkGetMain();
		main.setLogger(log);
		main.Main(args);
	}
	
	@Override
	protected void customClientBuilder(HttpClientBuilder builder) {
		builder.setUserAgent(UserAgents.CHROME);
	}
	
	@Override
	protected Document start() throws IOException, UserProcessException {
		MhbkGoTop p = new MhbkGoTop(client, context);
		Document out = p.process(null);
		return out;
	}

	@Override
	protected Document login(Document doc) throws IOException, UserProcessException {
		log.info("お客様番号入力処理開始");
		MhbkLoginInputUserNum pu = new MhbkLoginInputUserNum(client,context);
		pu.setUserNum(userNum);
		MhbkProcessRetryer pr = new MhbkProcessRetryer(pu,new MhbkLoginUserNumChecker());
		Document out1 = pr.tryProcess(doc);
		log.info("お客様番号入力処理終了");
		
//		log.fine("user num inputed doc="+out1.html());
		
		log.info("暗証番号入力処理開始");
		MhbkLoginInputPassword pp = new MhbkLoginInputPassword(client,context);
		pp.setPassword(password);
		Document out2 = pp.process(out1);
		new MhbkLoginPasswordChecker().check(out2);
		log.info("暗証番号入力処理終了");
		return out2;
	}

	@Override
	protected Document prepareDownload(Document doc) throws IOException, UserProcessException {
		MhbkGoDealDetail p = new MhbkGoDealDetail(client, context);
		Document out = p.process(doc);
		return out;
	}

	@Override
	protected File download(Document doc) throws IOException, UserProcessException {
		MhbkDownload p = new MhbkDownload(client, context, outFile);
		File f = p.process(doc);
		downloadDoc = p.getOutputDocuemnt();
		return f;
	}

	@Override
	protected void logout(Document doc) throws IOException, UserProcessException {
		MhbkLogout p = new MhbkLogout(client, context);
		p.process(downloadDoc);	// 入力documentとしてdownload画面のdocを使う
//		log.fine(out.html());
	}
	
	@SuppressWarnings("static-access")
	@Override
	protected Options getOptions() {
		Options options = new Options();
		Option out = new OutFileOption(false);options.addOption(out);
		options.addOption(OptionBuilder.hasArg(true).withArgName("number")
				.isRequired(true).withDescription("お客様番号(ご利用カードで確認可)")
				.create("user"));
		Option password = new PasswordOption(true); options.addOption(password);
		return options;
	}

	@Override
	protected void setParams(CommandLine cmd) {
		userNum = cmd.getOptionValue("user");
		this.password = cmd.getOptionValue(PasswordOption.OPT);
		outFile = new File(cmd.getOptionValue(OutFileOption.OPT, DEFAULT_MEISAI_FILENAME));
	}

}
